package com.douqu.game.core.config;

import com.douqu.game.core.config.common.CommonLvUp;
import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.e.E_InitDataType;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by bean on 2017/7/26.
 */
public class PlayerLvConfig extends CommonLvUp {


    /** 当前能携带进战斗的兵种数量 */
    public int battleSoldierCount;

    /**
     * 最小携带数量
     */
    public int minBattleSoldierCount;

    /**奖励*/
    public GoodsData[] reward;

    /**药水最大使用数量**/
    public Map<Integer, Integer> useLiquidMaxCount;

    @Override
    public boolean check()
    {
        if(id == 1)
        {
            List<InitDataConfig> initDataConfigList = InitDataConfig.getConfigByType(E_InitDataType.CARDS, LoadFactory.isTestLoad());
            if (initDataConfigList == null || initDataConfigList.size() == 0){
                LogUtils.error("InitDataConfig error ->" + "no have type " + E_InitDataType.CARDS.getMsg());
                return false;
            }

            InitDataConfig initDataConfig = initDataConfigList.get(0);
            if(initDataConfig.initCards.length < minBattleSoldierCount || initDataConfig.initCards.length > battleSoldierCount)
            {
                LogUtils.error("PlayerLvConfig check Error -> minBattleSoldierCount:" + minBattleSoldierCount + " battleSoldierCount:" + battleSoldierCount + " initCards:" + Arrays.toString(initDataConfig.initCards) + "  hashcode:" + hashCode());
                return false;
            }
        }

        return true;
    }


    @Override
    public boolean setVariable(String key, String value) {

        if("reward".equals(key))
        {
            reward = LoadUtils.loadGoodDataToArray(key, value);
            return LoadUtils.checkGoods(reward, this.getClass(), key, id);
        }else if("useLiquidMaxCount".equals(key)){
            useLiquidMaxCount = new ConcurrentHashMap<>();
            GoodsData[] datas = LoadUtils.loadGoodDataToArray(key, value);
            for(GoodsData data : datas){
                useLiquidMaxCount.put(data.type, data.id);
            }
            return true;
        }else{
            return super.setVariable(key, value);
        }
    }


        @Override
    public String toString() {
        return "PlayerLv{" +
                "exp=" + exp +
                ", battleSoldierCount=" + battleSoldierCount +
                ", minBattleSoldierCount=" + minBattleSoldierCount +
                ", reward=" + reward +
                "} " + super.toString();
    }
}
